package cn.zhj.mvpart.mvp

import android.os.Bundle
import android.view.InflateException
import androidx.appcompat.app.AppCompatActivity
import cn.zhj.mvpart.base.IActivity

abstract class BaseActivity<P:IPresenter> : AppCompatActivity(), IActivity ,IView {

    val mPresenter : P by lazy { createPresenter() }

    abstract fun createPresenter(): P

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        try {
            val layoutResID = initView(savedInstanceState)
            if (layoutResID != 0) {
                setContentView(layoutResID)
            }
        }catch(e: Exception) {
            if (e is InflateException) throw e
            e.printStackTrace()
        }
        mPresenter.onCreate(this)
        initData(savedInstanceState)
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter.onDestroy()
    }
}